<template>
	<view>
		<form @submit="addAddress">
			<uni-card :is-shadow="false">
				<view>
					<view class="address_item">
						<picker mode="selector" @change="bindShopChange" :range="shopList" range-key="name">
							<uni-easyinput disabled placeholder="选择门店、商场、园区、写字楼（必填）" v-model="chooseShopName"></uni-easyinput>
						</picker>
					</view>
					<view class="address_item">
						<uni-easyinput v-model="books.address" placeholder="单元、楼层、门牌号、档口号（必填）"></uni-easyinput>
					</view>
					<view class="address_item">
						<uni-easyinput v-model="books.linkman" placeholder="姓名"></uni-easyinput>
					</view>
					<view class="address_item">
						<uni-easyinput v-model="books.phone" placeholder="电话"></uni-easyinput>
					</view>
				</view>
				<view style="padding:10rpx;">
					<button form-type="submit"
						style="background-color:#FF4D4F;color:#F8F8F8;font-size:26rpx;">
						确定
					</button>
				</view>
			</uni-card>
		</form>
	</view>
</template>

<script>
	import { addBooks } from '@/util/express_api.js';
	import { post } from '@/util/request.js';
	
	export default {
		data() {
			return {
				books: {
					addressType: '',
					address: '',
					linkman: '',
					phone: '',
					shopId: '',
					shopName: ''
				},
				
				chooseShopName: '',
				shopList: []
			}
		},
		onLoad(options) {
			this.books.addressType = options.addressType;
			this.getShopList();
		},
		methods: {
			bindShopChange(e) {
				var shop = this.shopList[e.detail.value];
				this.books.shopId = shop.id;
				this.books.shopName = shop.name;
				this.chooseShopName = shop.name;
			}
			,addAddress(e) {
				if (this.books.shopId == '') {
					uni.showToast({
						icon: 'none',
						title: '请选择门店'
					});
					return false;
				}
				if (this.books.address == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入地址'
					});
					return false;
				}
				
				addBooks(this.books);
				
				uni.$emit('returnData',{books: this.books})
				uni.navigateBack({
					delta: 1
				});
			},
			getShopList() {
				post('express/shop/list', {}).then(res => {
					this.shopList = res.data;
				});
			}
		}
	}
</script>

<style>
.address_item {
	padding: 10rpx;
}
</style>
